package com.cgwx.systemservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cgwx.commonutils.MD5;
import com.cgwx.commonutils.result.SysResult;
import com.cgwx.springsecurity.annotation.Log;
import com.cgwx.springsecurity.entity.SysUser;
import com.cgwx.springsecurity.enums.BusinessType;
import com.cgwx.systemservice.entity.vo.SysUserQueryVo;
import com.cgwx.systemservice.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author GaoWei
 * @since 2023-10-12
 */

@Api(tags = "用户表前端控制器")
@RestController
@RequestMapping("/system/sysUser")
@CrossOrigin
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Log(title = "更改用户状态", businessType = BusinessType.STATUS)
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation("更改用户状态")
    @GetMapping("updateStatus/{id}/{status}")
    public SysResult updateStatus(
            @ApiParam(name = "id", value = "用户id")
            @PathVariable String id,
            @ApiParam(name = "status", value = "用户当前状态")
            @PathVariable Integer status) {
        sysUserService.updateStatus(id,status);
        return SysResult.ok();
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation("用户列表")
    @GetMapping("{page}/{limit}")
    public SysResult list(
            @ApiParam(name = "page", value = "当前页")
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页显示的记录数")
            @PathVariable Long limit,
            @ApiParam(name = "sysUserQueryVo", value = "待查询的用户条件Vo")
            SysUserQueryVo sysUserQueryVo) {
        //创建page对象
        Page<SysUser> pageParam = new Page<>(page,limit);
        //调用service方法
        IPage<SysUser> pageModel = sysUserService.selectPage(pageParam,sysUserQueryVo);
        return SysResult.ok(pageModel);
    }

    @Log(title = "添加用户", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysUser.add')")
    @ApiOperation("添加用户")
    @PostMapping("save")
    public SysResult save(
            @ApiParam(name = "user", value = "待添加的用户")
            @RequestBody SysUser user) {
        //把输入密码进行加密 MD5
        String encrypt = MD5.encrypt(user.getPassword());
        user.setPassword(encrypt);
        boolean is_Success = sysUserService.save(user);
        if(is_Success) {
            return SysResult.ok();
        } else {
            return SysResult.fail();
        }
    }

    @ApiOperation("根据id查询")
    @GetMapping("getUserById/{id}")
    public SysResult getUserById(
            @ApiParam(name = "id", value = "待查询的用户id")
            @PathVariable String id) {
        SysUser user = sysUserService.getById(id);
        return SysResult.ok(user);
    }

    @Log(title = "修改用户", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation("修改用户")
    @PostMapping("update")
    public SysResult update(
            @ApiParam(name = "user", value = "待修改的用户")
            @RequestBody SysUser user) {
        boolean is_Success = sysUserService.updateById(user);
        if(is_Success) {
            return SysResult.ok();
        } else {
            return SysResult.fail();
        }
    }

    @Log(title = "删除用户", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @ApiOperation("删除用户")
    @DeleteMapping("remove/{id}")
    public SysResult remove(
            @ApiParam(name = "id", value = "待删除的用户id")
            @PathVariable String id) {
        boolean is_Success = sysUserService.removeById(id);
        if(is_Success) {
            return SysResult.ok();
        } else {
            return SysResult.fail();
        }
    }
}

